import Vue from "vue";
import Vuex from "vuex";
import { getToken, setToken, removeToken,setUserInfoID,removeUserInfoID } from '@/utils/auth'
import { bidderLogin,bidderMobileLogin,getUserInfo,bidderLogout} from '@/api/user'
import { Message } from 'element-ui'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: getToken(),
    loginName:'',
    id:'',
    level:''
  },
  getters: {

    token: state => state.token

  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    LOGINNAME: (state, loginName) => {
      state.loginName = loginName
    },
    ID: (state, id) => {
      state.id = id
    },
    LEVEL: (state, level) => {
      state.level = level
    }
  },
  actions: {
    bidderLogin({ commit }, userInfo) {
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        bidderLogin({ username: username.trim(), password: password }).then(response => {
          const { data } = response
          // console.log(data.token)
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        }).catch(error => {
          if(error.code == 40001){
             Message({
                message: error.msg ,
                type: 'warning'
            })
          }
        })
      })
    },
    bidderMobileLogin({ commit }, userInfo) {
      const { mobile, code } = userInfo
      return new Promise((resolve, reject) => {
        bidderMobileLogin({ mobile: mobile.trim(), code: code }).then(response => {
          const { data } = response;
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        }).catch(error => {
          if(error.code == 40001){
            Message({
               message: error.msg ,
               type: 'warning'
            })
          }
        })
      })
    },

    getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          const { data } = response
          if (response.code !== 200) {
            removeToken(); //删除token
            // return reject(response.msg);
          }else{
            setUserInfoID(JSON.stringify(data))
            commit('ID', data.id)
            commit('LOGINNAME', data.username)
            commit('LEVEL', data.level)
            // setUserInfoID(JSON.stringify({id:data.id,loginName:data.username,level:data.level,}))
            
            resolve()
          }
        }).catch(error => {
          reject(error);
          if(error.code == 401){
            Message({
              message: error.msg ,
              type: 'error'
            });
          }
            removeToken();
        })
      })
    },

    bidderLogout({ commit }) {
      return new Promise((resolve, reject) => {
        bidderLogout().then(response => {
          removeToken();
          removeUserInfoID();
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },

  },
  modules: {},
});
